
global data "/Users/ap18845/Documents/Travaux/David Abrams2/JEEA final/data"
global results "/Users/ap18845/Documents/Travaux/David Abrams2/JEEA final/test2"

cd "$data"
*cd "C:\Users\Emeric.Henry\Dropbox\North Carolina\NEW data"


use data_clean, clear







**** Create controls for fixed effect regressions

global fecontrol  elected tenure_ju  num_times_seen_sofar first_offense_flag   femaleflag blackflag minor_flag  senior_flag age  
global contgraphs elected tenure_ju  num_times_seen_sofar first_offense_flag  femaleflag blackflag minor_flag  senior_flag age 


****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
**** Additional variable we need
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************


sort ID_AT dateID
by ID_AT: gen order_AT=_n

sort ID_DA dateID
by ID_DA: gen order_DA=_n

gen order_ju_AT_elected=order_ju_AT*elected
gen order_ju_DA_elected=order_ju_DA*elected

gen ID_ju_AT=ID_AT*1000+IDjudge
gen ID_ju_DA=ID_DA*1000+IDjudge


****************************************************************************************
****************************************************************************************
**** Additional variables on elections


rename JudgeFirst judgefirst 
rename Judge judge 
rename JudgeMiddle judgemiddle 

drop _merge*
merge m:1 judgefirst judge judgemiddle  using election_district, keepusing(electiondate1_k electiondate2_k percentagevote1_k percentagevotes2_k party)

gen date_election1=date(electiondate1_k, "DMY")
gen date_election2=date(electiondate2_k, "DMY")
drop electiondate1_k electiondate2_k
foreach x of var percentagevote1_k percentagevotes2_k{
replace `x'=subinstr(`x', ",",".",.)
replace `x'=subinstr(`x', "%","",.)
destring `x', replace
}

gen year_election2=year(date_election2)
gen month_election2=month(date_election2)

gen year_election1=year(date_election1)
gen month_election1=month(date_election1)


rename  judgefirst  JudgeFirst
rename  judge Judge
rename  judgemiddle JudgeMiddle


gen calendar_month=month+12*(year-1998)

gen pre_electoral=(year==year_election2 & month<month_election2) | (year==year_election2-1) | /*
*/(year==year_election2-2 & month>=month_election2) | /*
*/(year==year_election1 & month<month_election1) | (year==year_election1-1) /*
*/| (year==year_election1-2 & month>=month_election1)
* pre-electoral period is the two year before election
gen pre_electoral_elected=pre_electoral*elected



****************************************************************************************
****************************************************************************************
**** Additional variable on characteristics of the district
drop _merge
merge m:1 districtID year County using characteristics_county
drop if _merge==2

****************************************************************************************
****************************************************************************************
**** Additional variables on order of decisions

gen order_ju_di_elu=order_ju_di*elected
gen order_ju_di_pre_elec=order_ju_di*pre_electoral
gen order_ju_di_pre_elec_elu=order_ju_di*pre_electoral*elected

bys IDjudge districtID: egen max_order=max(order_ju_di)


****************************************************************************************
****************************************************************************************
**** Additional variables on distance between decision and average decisions


*compute mean among "old judges" by district, district crime_trunc or district crime
drop maxSENT_wind
sum maxSENT, d
gen maxSENT_wind=maxSENT
replace maxSENT_wind=r(p95) if maxSENT>r(p95)


gen maxSENT_wind_old=maxSENT_wind
replace maxSENT_wind_old=. if youngjudge==1

bys districtID : egen mean_di_cr_out=mean(maxSENT_wind_old)
bys districtID IDcrime_trunc: egen mean_di_cr_out2=mean(maxSENT_wind_old)
bys districtID IDcrime: egen mean_di_cr_out3=mean(maxSENT_wind_old)

gen distance1=maxSENT_wind-mean_di_cr_out
gen abs_distance1=abs(maxSENT_wind-mean_di_cr_out)
gen distance2=maxSENT_wind-mean_di_cr_out2
gen abs_distance2=abs(maxSENT_wind-mean_di_cr_out2)
gen distance3=maxSENT_wind-mean_di_cr_out3
gen abs_distance3=abs(maxSENT_wind-mean_di_cr_out3)


*compute other mean by district crime
*among "old elected judges" 
gen maxSENT_wind_old2=maxSENT_wind_old 
replace maxSENT_wind_old2=. if elected==0

bys districtID IDcrime: egen mean_di_cr_out4=mean(maxSENT_wind_old2)

gen distance4=maxSENT_wind-mean_di_cr_out4
gen abs_distance4=abs(maxSENT_wind-mean_di_cr_out4)


*among "old NON elected judges" 
gen maxSENT_wind_old3=maxSENT_wind_old 
replace maxSENT_wind_old3=. if elected==1

bys districtID IDcrime: egen mean_di_cr_out5=mean(maxSENT_wind_old3)

gen distance5=maxSENT_wind-mean_di_cr_out5
gen abs_distance5=abs(maxSENT_wind-mean_di_cr_out5)




*among judges with more than 5 year experience 
gen maxSENT_wind_old6=maxSENT_wind_old 
replace maxSENT_wind_old6=. if  StartYear>1993 

bys districtID IDcrime: egen mean_di_cr_out6=mean(maxSENT_wind_old6)

gen distance6=maxSENT_wind-mean_di_cr_out6
gen abs_distance6=abs(maxSENT_wind-mean_di_cr_out6)

*among "old judges" and young judges after their 400 first decisions
* by district, district crime_trunc or district crime

gen maxSENT_wind_oth=maxSENT_wind
replace maxSENT_wind_oth=. if youngjudge==1 & order_ju_di<=400

bys districtID IDcrime: egen mean_di_cr_out7=mean(maxSENT_wind_oth)


gen distance7=maxSENT_wind-mean_di_cr_out7
gen abs_distance7=abs(maxSENT_wind-mean_di_cr_out7)

bys IDjudge districtID: egen max_in_distr=max(order_ju_di)







cd "$results"


****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
** Figure 2: graph rotation
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************



preserve

********************************************************************************
* recreate main district

tab districtID, gen(dist_)
forval x=1/50{
bys IDjudge month year: egen mean_dist_`x'=mean(dist_`x')
}
drop dist_*

gen main_dist=1
gen transit=mean_dist_1
forval x=2/50{
replace main_dist=`x' if mean_dist_`x'>transit
replace transit=mean_dist_`x' if mean_dist_`x'>transit
}
drop transit mean_dist_*

********************************************************************************
* graph


bys IDjudge month year: gen transit=_n
bys IDjudge month year: egen nb_case_month=max(transit)
drop transit
sum nb_case_month, d


keep  if  match=="district"
drop if nb_case_month<10
keep IDjudge month year district_master time youngjudge
duplicates drop

sort IDjudge  year month
gen transition=district_master!=district_master[_n-1] & time==time[_n-1]+1 & IDjudge==IDjudge[_n-1]
replace transition=. if time!=time[_n-1]+1 | IDjudge!=IDjudge[_n-1]

bys month youngjudge: egen mean_transit=mean(transition)

keep month mean_transit youngjudge
duplicates drop

gen month2=month*3+1
replace month=month*3

tw(bar mean_transit month if youngjudge==0, color(gs10) )(bar mean_transit month2 if youngjudge==1, color(gs4) ), /*
title("Probability of Moving According to Master Schedule") 
*/ ylabel(0 "0" 0.2 "0.2" 0.4 "0.4" 0.6 "0.6" 0.8 "0.8" 1 "1") ytitle("Probability") /*
*/ xtitle("Month of Decision") xlabel(3.5 "1" 6.5 "2" 9.5 "3" 12.5 "4" 15.5 "5" 18.5 "6" 21.5 "7" 24.5 "8" 27.5 "9" 30.5 "10" 33.5 "11" 36.5 "12") legend(order(1 "Junior judges" 2 "Senior judges")) /*
*/ graphregion(icolor(white) color(white) margin(medlarge)) plotregion(lcolor(black) lwidth(vthin))
graph export fig2_rotation.pdf, replace

restore



****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
** Figure 3: Spatial variation in sentencing
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************


preserve


gen all=1
gen di=.
gen mean_all=.
gen mean_property=.
gen mean_violent=.
gen mean_drugs=.
gen mean_fraud=.
gen mean_other=.

gen high_all=.
gen high_property=.
gen high_violent=.
gen high_drugs=.
gen high_fraud=.
gen high_other=.

gen low_all=.
gen low_property=.
gen low_violent=.
gen low_drugs=.
gen low_fraud=.
gen low_other=.

local i=1
forval y=1/50{
replace di=`y' if _n==`i'
foreach x of var all property-other{
sum maxSENT_wind_old if `x'==1 & districtID==`y'
replace mean_`x'=r(mean) if _n==`i'
replace high_`x'=r(mean)+invttail(r(N)-1,0.05)*(r(sd) / sqrt(r(N))) if _n==`i'
replace low_`x'=r(mean)-invttail(r(N)-1,0.05)*(r(sd) / sqrt(r(N))) if _n==`i'
}
local i=`i'+1
}

tw(bar mean_all di)(rcap high_all low_all di)/*
*/, xtitle("Distict ID") ytitle("Mean sentence (judges starting before 1998)")/*
*/legend(off)/*
*/ graphregion(icolor(white) color(white) margin(medlarge)) plotregion(lcolor(black) lwidth(vthin))/*
*/ title("All crimes")
graph export fig3_all.pdf, replace

tw(bar mean_drugs di)(rcap high_drugs low_drugs di)/*
*/, xtitle("Distict ID") ytitle("Mean sentence (judges starting before 1998)")/*
*/legend(off)/*
*/ graphregion(icolor(white) color(white) margin(medlarge)) plotregion(lcolor(black) lwidth(vthin))/*
*/ title("Drug crimes")
graph export fig3_drugs.pdf, replace

tw(bar mean_property di)(rcap high_property low_property di)/*
*/, xtitle("Distict ID") ytitle("Mean sentence (judges starting before 1998)")/*
*/legend(off)/*
*/ graphregion(icolor(white) color(white) margin(medlarge)) plotregion(lcolor(black) lwidth(vthin))/*
*/ title("Property crimes")
graph export fig3_property.pdf, replace

tw(bar mean_violent di)(rcap high_violent low_violent di)/*
*/, xtitle("Distict ID") ytitle("Mean sentence (judges starting before 1998)")/*
*/legend(off)/*
*/ graphregion(icolor(white) color(white) margin(medlarge)) plotregion(lcolor(black) lwidth(vthin))/*
*/ title("Violent crimes")
graph export fig3_violence.pdf, replace
restore





****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
** Figure 4: Spatial variation in sentencing
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************

sort districtID IDcrime

tab districtID, g(distr)
tab IDjudge, g(judge)

gen fe_full=0
gen se_full=0
gen fe_judge=0
gen se_judge=0

quietly reghdfe  maxSENT_wind  distr1-distr50 judge1-judge157 i.year num_times_seen_sofar first_offense_flag  femaleflag blackflag minor_flag  senior_flag age    if  match=="district"  & district_main==districtID & youngjudge==0 ,  a(IDcrime) 
forval x=2/50{
replace fe_full=_b[distr`x']   if districtID==`x' 
replace se_full=_se[distr`x'] if districtID==`x' 
}

forval x=2/157{
replace fe_judge=_b[judge`x']   if IDjudge==`x' 
replace se_judge=_se[judge`x'] if IDjudge==`x' 
}

bysort districtID: egen n_dis=sum(id)

gen fe_high=fe_full +invttail(n_dis-1,0.01)*(se_full/ sqrt(n_dis))
gen fe_low=fe_full-invttail(n_dis-1,0.01)*(se_full/ sqrt(n_dis))

bysort IDjudge: egen n_jud=sum(id)

gen judge_high=fe_judge +invttail(n_jud-1,0.01)*(se_judge/ sqrt(n_jud))
gen judge_low=fe_judge -invttail(n_jud-1,0.01)*(se_judge/ sqrt(n_jud))

****************************************************************************************
** Left panel: by district
****************************************************************************************

preserve

sort districtID
drop if districtID[_n-1]==districtID  
drop if districtID==.

tw(bar fe_full districtID,   yscale(range(-110 310)))(rcap fe_high fe_low districtID)/*
*/, xtitle("Distict ID") ytitle("District FE  ")/*
*/legend(off)/*
*/ graphregion(icolor(white) color(white) margin(medlarge)) plotregion(lcolor(black) lwidth(vthin))/*
*/
graph export fig4_spatial.pdf, replace


restore

****************************************************************************************
** Right panel: by judge
****************************************************************************************

preserve

sort IDjudge
drop if IDjudge[_n-1]==IDjudge 
drop if IDjudge==.
drop if youngjudge==1
drop if n_jud<10
drop if fe_jud==0

gen njud=_n

ta fe_judge

count

tw(bar fe_judge njud,  yscale(range(-110 310)))(rcap judge_high judge_low njud)/*
*/, xtitle("Judge ID") ytitle("Judge FE ")/*
*/legend(off)/*
*/ graphregion(icolor(white) color(white) margin(medlarge)) plotregion(lcolor(black) lwidth(vthin))/*
*/
graph export fig4_judge.pdf, replace


restore








****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
** Table 1 Panel A: descriptive statistics
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************

**Prepare and label variables

gen white_def=(realrace=="W")
gen race_other =1-white_def-black

label var femaleflag "female"
label var blackflag "black"
label var minor_flag  "minor"
label var num_times_seen  "criminal history"
label var first_offense_flag "first offense"
label var white_def "white"
label var age  "age"
label var race_other "other race"

**** for sentences

label var maxSENT  "sentence (days)"
label var alt_maxSENT  "active sentence (days)"

**** for judges

gen tenure=tenure_ju 
replace  tenure=. if tenure_ju <0

label var elected  "Judge in district of election"

gen plea=1-trial
label var plea "Resolved by plea bargain"


global dem femaleflag  blackflag white_def race_other   minor_flag  age num_times_seen first_offense  
global jud tenure elected plea
global sent maxSENT  alt_maxSENT 

gen work_sample=(youngjudge==1 & max_in_distr>400 & order_ju_di<=400)



****************************************************************************************
** Panel A
****************************************************************************************
 

eststo domestic: quietly estpost summarize ///
    $dem $jud $sent if youngjudge == 1  
eststo foreign: quietly estpost summarize ///
    $dem $jud $sent if youngjudge == 0  
	eststo diff: quietly estpost summarize ///
    $dem $jud $sent if work_sample == 1

esttab domestic foreign diff, ///
cells("mean(pattern(1 1 1) fmt(2)) sd(pattern(1 1 1))  ") ///
label

local tabnom  "tab1_panelA"

esttab  domestic foreign diff using `tabnom'.tex, replace ///
cells("mean(pattern(1 1 1) fmt(2)) sd(pattern(1 1 1))") ///
label





****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
** Table 1  Panel B: descriptive statistics district level
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************

**** First step create number of judges per district, keeping only main district
**** First step is to keep only observations taken in the main district

preserve

sort month districtID IDjudge

gen chge_judge=0
replace chge_judge=1 if  IDjudge[_n-1]!=IDjudge

bysort districtID month: egen nber_judge=sum(chge_judge)
sort districtID month
drop if month[_n-1]!= month

bysort districtID: egen district_size1=mean(nber_judge)
bysort districtID : gen id1=_n
drop if id1!=1

gen district_size=round(district_size1,1)

keep districtID  district_size
save temp_size, replace

restore

***** Create number attorneys

preserve

sort month districtID ID_AT

drop if ID_AT==.

gen chge_at=0
replace chge_at=1 if  ID_AT[_n-1]!=ID_AT

bysort districtID month: egen nber_at=sum(chge_at)
sort districtID month
drop if month[_n-1]!= month

bysort districtID: egen district_size_at1=mean(nber_at)
bysort districtID : gen id1=_n
drop if id1!=1

gen district_size_at=round(district_size_at1,1)

keep districtID  district_size_at
save temp_size_at, replace

restore

***** create nber district_attorneys

preserve

sort month districtID ID_DA

drop if ID_DA==.

gen chge_da=0
replace chge_da=1 if  ID_DA[_n-1]!=ID_DA

bysort districtID month: egen nber_da=sum(chge_da)
sort districtID month
drop if month[_n-1]!= month

bysort districtID: egen district_size_da1=mean(nber_da)
bysort districtID : gen id1=_n
drop if id1!=1

gen district_size_da=round(district_size_da1,1)


keep districtID  district_size_da
save temp_size_da, replace

restore

preserve

drop _merge
merge n:n districtID using temp_size 
drop _merge
merge n:n districtID using temp_size_at 
drop _merge
merge n:n districtID using temp_size_da 
drop _merge


***** create the general variables at the state level

gen dem=democrat
gen rep=republican
gen capac=capacity_snap_beds
replace capac=. if capac==-1
gen crowd=totalpop_snap
replace crowd=. if crowd==-1

sort districtID year County 

replace dem=. if districtID[_n-1]== districtID & County[_n-1]== County & year[_n-1]== year
replace rep=. if districtID[_n-1]== districtID & County[_n-1]== County & year[_n-1]== year
replace capac=. if districtID[_n-1]== districtID & County[_n-1]== County & year[_n-1]== year
replace crowd=. if districtID[_n-1]== districtID & County[_n-1]== County & year[_n-1]== year

bysort districtID year: egen dem_year=sum(dem)
bysort districtID year: egen rep_year=sum(rep)
bysort districtID year: egen capacity_year=sum(capac)
bysort districtID year: egen crowd_year=sum(crowd)

sort districtID year
replace dem_year=. if districtID[_n-1]== districtID & year[_n-1]== year
replace dem_year=. if dem_year==0
replace rep_year=. if districtID[_n-1]== districtID & year[_n-1]== year
replace rep_year=. if rep_year==0
replace capacity_year=. if districtID[_n-1]== districtID & year[_n-1]== year
replace capacity_year=. if capacity_year==0
replace crowd_year=. if districtID[_n-1]== districtID & year[_n-1]== year
replace crowd_year=. if crowd_year==0

bysort districtID: egen dem_dis=mean(dem_year)
bysort districtID: egen rep_dis=mean(rep_year)
bysort districtID: egen capacity_dis=mean(capacity_year)
bysort districtID: egen crowd_dis=mean(crowd_year)

gen left_dist=dem_dis/(dem_dis+rep_dis)
gen overcrowd_dis= crowd_dis / capacity_dis

bysort districtID: egen unemp_dis=mean(unemployed)
bysort districtID: egen black_dis=mean(black)
bysort districtID: egen white_dis=mean(white)
bysort districtID: egen sex_dis=mean(sex)
bysort districtID: egen farm_dis=mean(farm)

label var district_size "n. judge"
label var district_size_at "n. attorney"
label var district_size_da "n. district_attorney"
label var left_dis "share Democrat in presidential"
label var overcrowd_dis "prison overcrowding"
label var unemp_dis "unemployment rate"
label var black_dis "prop black"
label var white_dis "prop white"
label var sex_dis "prop female"

global jud_dis district_size district_size_da district_size_at overcrowd_dis
global pop_dis left_dis  unemp_dis black_dis white_dis sex_dis 

sort districtID
drop if districtID[_n-1]==districtID
drop if districtID==.



****************************************************************************************
** Panel B
****************************************************************************************
 
local tabnom  "tab1_panelB"

eststo domestic: quietly estpost summarize ///
    $jud_dis $pop_dis

esttab domestic,  ///
 cells("mean sd min max") noobs ///
 label

esttab domestic using `tabnom'.tex, replace ///
 cells("mean sd min max") noobs ///
label

restore




****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
** Table 2: balancing tests
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************




gen Assault=CrimeClassChargeTrue=="Assault"
gen Burglary=CrimeClassChargeTrue=="Burglary"
gen Drug=CrimeClassChargeTrue=="Dangerous Drugs"
gen Fraud=CrimeClassChargeTrue=="Fraud"
gen Larceny=CrimeClassChargeTrue=="Larceny"
gen Robbery=CrimeClassChargeTrue=="Robbery"

label var order_ju_di "Case number in district"
label var order_ju_di_elu "Case number in district * Home"
label var elected "Home"
label var order_ju_cr "Case number in crime category"
label var order_ju "Case number"
label var plea "Plea"
 
gen nber_times=num_times_seen_sofar
bysort  person_name_group: gen id_person=_n
replace nber_times=nber_times[_n-1] if id_person>1
drop id_person

****************************************************************************************
** Panel A

preserve
keep if youngjudge==1
keep if max_in_distr>400
keep if order_ju_di<=400

keep if distance3!=.
count
eststo clear 
foreach x of var  first_offense_flag nber_times  femaleflag blackflag age minor_flag  senior_flag {
qui eststo: reg `x' order_ju_di order_ju_di_elu elected order_ju_cr order_ju, cluster(IDjudge) a(IDjudge)
}
local regnom    "tab2_panelA"
esttab using  `regnom',  se  ar2 starlevels({$^{*}$} 0.1 {$^{**}$} 0.05 {$^{***}$} 0.01)  tex  label replace keep(order_ju_di order_ju_di_elu elected order_ju_cr order_ju) /*
*/mtitles("First offense" "Nber prior" "Female" "Black" "Age" "Minor" "Senior"  ) 
esttab,  compress ar2 starlevels(* 0.1 ** 0.05 *** 0.01)  b(%4.3f) se(%4.3f) label   keep(order_ju_di order_ju_di_elu elected order_ju_cr order_ju) mtitles("First offense" "Nber prior" "Female" "Black" "Age" "Minor" "Senior" )
eststo clear 
restore



****************************************************************************************
** Panel B

preserve
keep if youngjudge==1
keep if max_in_distr>400
keep if order_ju_di<=400

keep if distance3!=.
count
local regnom    "tab2_panelB"

foreach x of var  Assault-Robbery{
qui eststo: reg `x' order_ju_di order_ju_di_elu elected order_ju_cr order_ju, cluster(IDjudge) a(IDjudge)
}
esttab using  `regnom',  se(%4.3f)  ar2 starlevels({$^{*}$} 0.1 {$^{**}$} 0.05 {$^{***}$} 0.01)  tex  label replace keep(order_ju_di order_ju_di_elu elected order_ju_cr order_ju) 
esttab,  compress ar2 starlevels(* 0.1 ** 0.05 *** 0.01)  b(%4.3f) se(%4.3f) label   keep(order_ju_di order_ju_di_elu elected order_ju_cr order_ju)
eststo clear 
restore








****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
** Table 3: main
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************

preserve
keep if youngjudge==1
keep if max_in_distr>400
keep if order_ju_di<=400


sum distance3
local mean=r(mean)
local sd=r(sd)

qui eststo: reg distance3 order_ju_di order_ju_di_elu elected order_ju_cr order_ju,  cluster(IDjudge) a(IDjudge)

sum abs_distance3
local mean=r(mean)
local sd=r(sd)

qui eststo: reg abs_distance3 order_ju_di order_ju_di_elu elected order_ju_cr order_ju,  cluster(IDjudge) a(IDjudge)

tab IDjudge, g(juge_identifier)

foreach x in 10  25  50  75  90{
qui eststo: qreg2 distance3 order_ju_di order_ju_di_elu elected order_ju_cr order_ju juge_identifier*, q(`x') c(IDjudge)
}


local regnom    "tab3"
esttab using  `regnom',   b(%4.3f) se(%4.3f)  r2 starlevels({$^{*}$} 0.1 {$^{**}$} 0.05 {$^{***}$} 0.01)  tex  label replace  keep(order_ju_di order_ju_di_elu elected order_ju_cr order_ju) mtitles(OLS "|distance|" q10 q25 q50 q75 q90 )
esttab,  compress ar2 starlevels(* 0.1 ** 0.05 *** 0.01)  b(%4.3f) se(%4.3f) label   keep(order_ju_di order_ju_di_elu elected order_ju_cr order_ju) mtitles(OLS "|distance|" q10 q25 q50 q75 q90)
eststo clear 

restore




****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
** Table 4: robust
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
eststo clear 

****************************************************************************************
*  norm home senior judges

preserve
keep if youngjudge==1
keep if max_in_distr>400
keep if order_ju_di<=400

sum abs_distance4
local mean=r(mean)
local sd=r(sd)

qui eststo: reg abs_distance4 order_ju_di order_ju_di_elu elected order_ju_cr order_ju, cluster(IDjudge) a(IDjudge)

restore


****************************************************************************************
* rolling norm

gen mean_di_cr_out11=.

forval x=2000/2010{
local y=`x'-1
local z=`x'-2

gen transit=maxSENT_wind_old
replace transit=. if year!=`y' & year!=`z'
bys districtID IDcrime: egen roulant=mean(transit)
replace mean_di_cr_out11=roulant if year==`x'
drop transit roulant
}
gen abs_distance11=abs(maxSENT_wind-mean_di_cr_out11)



preserve
keep if youngjudge==1
keep if max_in_distr>400
keep if order_ju_di<=400

sum abs_distance11
local mean=r(mean)
local sd=r(sd)

qui eststo: reg abs_distance11 order_ju_di order_ju_di_elu elected order_ju_cr order_ju,  cluster(IDjudge) a(IDjudge)
restore


****************************************************************************************
* norm exp. senior judges

preserve
keep if youngjudge==1
keep if max_in_distr>400
keep if order_ju_di<=400

sum abs_distance6
local mean=r(mean)
local sd=r(sd)


qui eststo: reg abs_distance6 order_ju_di order_ju_di_elu elected order_ju_cr order_ju, cluster(IDjudge) a(IDjudge)
restore



****************************************************************************************
* 300 decisions

preserve
keep if youngjudge==1
keep if max_in_distr>300
keep if order_ju_di<=300

sum abs_distance3
local mean=r(mean)
local sd=r(sd)

qui eststo: reg abs_distance3 order_ju_di order_ju_di_elu elected order_ju_cr order_ju, cluster(IDjudge) a(IDjudge)
restore


****************************************************************************************
* 500 decisions

preserve
keep if youngjudge==1
keep if max_in_distr>500
keep if order_ju_di<=500

sum abs_distance3
local mean=r(mean)
local sd=r(sd)

qui eststo: reg abs_distance3 order_ju_di order_ju_di_elu elected order_ju_cr order_ju, cluster(IDjudge) a(IDjudge)
restore

****************************************************************************************
* control socio-demo

preserve
keep if youngjudge==1
keep if max_in_distr>400
keep if order_ju_di<=400

sum abs_distance3
local mean=r(mean)
local sd=r(sd)

qui eststo: reghdfe abs_distance3 order_ju_di order_ju_di_elu elected order_ju_cr order_ju i.month first_offense_flag   femaleflag blackflag minor_flag  senior_flag age,  vce(cluster IDjudge) a(year IDjudge)
restore


****************************************************************************************
* active part

sum alt_maxSENT, d
gen alt_maxSENT_wind=alt_maxSENT
replace alt_maxSENT_wind=r(p95) if alt_maxSENT>r(p95)

gen alt_maxSENT_wind_old=alt_maxSENT_wind
replace alt_maxSENT_wind_old=. if youngjudge==1
bys districtID IDcrime: egen mean_di_cr_out8=mean(alt_maxSENT_wind_old)
gen distance8=alt_maxSENT_wind-mean_di_cr_out8
gen abs_distance8=abs(alt_maxSENT_wind-mean_di_cr_out8)


gen inactive=maxSENT_wind-alt_maxSENT_wind
gen inactive_old=inactive
replace inactive_old=. if youngjudge==1
bys districtID IDcrime: egen mean_di_cr_out9=mean(inactive_old)
gen distance9=inactive-mean_di_cr_out9
gen abs_distance9=abs(inactive-mean_di_cr_out9)


preserve
keep if youngjudge==1
keep if max_in_distr>400
keep if order_ju_di<=400

sum abs_distance8
local mean=r(mean)
local sd=r(sd)

qui eststo: reg abs_distance8 order_ju_di order_ju_di_elu elected order_ju_cr order_ju, cluster(IDjudge) a(IDjudge)
restore


****************************************************************************************
* probation

gen maxProbation_old=maxProbation
replace maxProbation_old=. if youngjudge==1
bys districtID IDcrime: egen mean_di_cr_out10=mean(maxProbation_old)
gen distance10=maxProbation-mean_di_cr_out10
gen abs_distance10=abs(maxProbation-mean_di_cr_out10)


preserve
keep if youngjudge==1
keep if max_in_distr>400
keep if order_ju_di<=400

sum plea
local mean=r(mean)
local sd=r(sd)

eststo: reg plea order_ju_di order_ju_di_elu elected order_ju_cr order_ju, cluster(IDjudge) a(IDjudge)
restore



local regnom    "tab4"

esttab using  `regnom',   b(%4.3f) se(%4.3f) ar2 starlevels({$^{*}$} 0.1 {$^{**}$} 0.05 {$^{***}$} 0.01)  tex  label replace keep(order_ju_di order_ju_di_elu elected order_ju_cr order_ju  )  mtitles("Norm senior elected" "Rolling norm" "Norm exp seniors"  "300" "500" "Control socio-demo"  "Active+inactive part"  "Plea")
esttab,  compress ar2 starlevels(* 0.1 ** 0.05 *** 0.01)  b(%4.3f) se(%4.3f) label   keep(order_ju_di order_ju_di_elu elected order_ju_cr order_ju  ) mtitles("Norm senior elected" "Rolling norm" "Norm exp seniors" "300" "500" "No condition"  "Control socio-demo"  "Active+inactive part"  "Plea")
eststo clear 





****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
** Appendix Table 8: Robustness of convergence to local norm
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************



************************************************************************************************************ step 1: defining spells
***********************************************************************************************************

preserve

keep if youngjudge==1
 
gen bla=1
bys IDjudge districtID month year: egen nb_decision=sum(bla)

keep IDjudge districtID month year nb_decision
duplicates drop


gen semester=(year-1997)*2
replace semester=semester+1 if month>6

sort IDjudge year month


bys IDjudge  districtID semester: egen nb_decision_di_sem=sum(nb_decision)
gen bla=1
bys IDjudge  districtID semester: egen nb_mois_di_sem=sum(bla)


sort IDjudge districtID year month

* spell: at least 10 decisions and at least 3 months in the semester
gen transit=IDjudge!=IDjudge[_n-1] | districtID!=districtID[_n-1] | /*
*/ (semester!=semester[_n-1] & nb_mois_di_sem>=3 & nb_decision_di_sem>10)


sort IDjudge districtID year month
by IDjudge districtID: gen spell=sum(transit)
drop transit*


** restrict definition by removing periods when there is no clear definition of the spell
gen spell_restrict=spell
gen transit=nb_mois_di_sem<3 | nb_decision_di_sem<=10
replace spell_restrict=. if transit==1
by IDjudge districtID: egen transit2=min(spell_restrict)
replace spell_restrict=spell_restrict-transit2+1 if spell_restrict!=.
replace spell_restrict=spell_restrict[_n-1] if districtID==districtID[_n-1] & IDjudge==IDjudge[_n-1] /*
*/ & spell_restrict==. & transit[_n-1]==0 & semester==semester[_n-1]+1
replace spell_restrict=spell_restrict[_n-2] if districtID==districtID[_n-2] & IDjudge==IDjudge[_n-2] /*
*/ & spell_restrict==. & transit[_n-2]==0 & semester==semester[_n-2]+1
replace spell_restrict=spell_restrict[_n+1] if districtID==districtID[_n+1] & IDjudge==IDjudge[_n+1] /*
*/ & spell_restrict==. & transit[_n+1]==0 & (month==month[_n+1]-1 | month==12 & month[_n+1]==1)

drop transit*


*** identify last spell
bys IDjudge semester: egen transit=max(nb_decision_di_sem)
bys IDjudge semester: egen transit2=max(nb_mois_di_sem)


gen transit3=districtID if nb_decision_di_sem==transit & nb_mois_di_sem==transit2
bys  IDjudge semester: egen main_distr=mean(transit3)
bys  IDjudge semester: egen main_distr2=max(transit3)
count if main_distr!=main_distr2
count if main_distr==main_distr2

gen last_visited=main_distr[_n-1] if IDjudge==IDjudge[_n-1] & semester==semester[_n-1]+1
forval x=1/20{
replace last_visited=main_distr[_n-`x'] if IDjudge==IDjudge[_n-`x'] & semester==semester[_n-`x']+1 & last_visited==.
}
gen last_visited2=main_distr[_n-1] if IDjudge==IDjudge[_n-1] & semester==semester[_n-1]+2
forval x=1/20{
replace last_visited2=main_distr[_n-`x'] if IDjudge==IDjudge[_n-`x'] & semester==semester[_n-`x']+2 & last_visited2==.
}


save spell_simple, replace
restore

***********************************************************************************************************
* step 2: merge
***********************************************************************************************************

drop _merge
merge m:1 IDjudge districtID year month using spell_simple

***********************************************************************************************************
* step 3: regressions
***********************************************************************************************************


preserve
drop spell
rename spell_restrict spell

bys IDjudge districtID: egen maxspell=max(spell)


keep if youngjudge==1
keep if maxspell>=4
keep if spell<=4
count

gen spell1=spell==1
gen spell2=spell==2
gen spell3=spell==3
gen spell_elu1=spell1*elected
gen spell_elu2=spell2*elected
gen spell_elu3=spell3*elected

gen spell_elu=spell*elected


drop bla
gen bla=1
sort IDjudge districtID spell year month dateID order_ju_di
by IDjudge districtID spell: gen order_in_spell=sum(bla)

gen spell4=spell==4

gen order_in_spell1= order_in_spell*spell1
gen order_in_spell2= order_in_spell*spell2
gen order_in_spell3= order_in_spell*spell3
gen order_in_spell4= order_in_spell*spell4

gen order_in_spell1_elu= order_in_spell1*elected
gen order_in_spell2_elu= order_in_spell2*elected
gen order_in_spell3_elu= order_in_spell3*elected
gen order_in_spell4_elu= order_in_spell4*elected

***********************************************************************************************************
* col1: OLS

sum distance3
local mean=r(mean)
local sd=r(sd)

qui eststo: reg distance3 spell1 spell2 spell3 spell_elu1 spell_elu2 spell_elu3 order_in_spell1-order_in_spell4_elu elected order_ju_cr order_ju,  cluster(IDjudge)  a(IDjudge)


***********************************************************************************************************
* col2: |distance|

sum abs_distance3
local mean=r(mean)
local sd=r(sd)

qui eststo: reg abs_distance3 spell1 spell2 spell3 spell_elu1 spell_elu2 spell_elu3 order_in_spell1-order_in_spell4_elu elected order_ju_cr order_ju,  cluster(IDjudge)  a(IDjudge)


***********************************************************************************************************
* col3-7: q10-q90

tab IDjudge, g(juge_identifier)

foreach x in 10  25  50  75  90{
qui eststo: qreg2 distance3 spell1 spell2 spell3 spell_elu1 spell_elu2 spell_elu3 order_in_spell1-order_in_spell4_elu elected order_ju_cr order_ju juge_identifier* , q(`x') c(IDjudge)
}


local regnom    "tab8"
esttab using  `regnom',   b(%4.3f) se(%4.3f)  r2 starlevels({$^{*}$} 0.1 {$^{**}$} 0.05 {$^{***}$} 0.01)  tex  label replace  keep(spell1 spell2 spell3 spell_elu1 spell_elu2 spell_elu3 order_in_spell1 order_in_spell2 order_in_spell3 order_in_spell4 order_in_spell1_elu order_in_spell2_elu order_in_spell3_elu order_in_spell4_elu elected order_ju_cr order_ju) mtitles(OLS "|distance|" q10 q25 q50 q75 q90 )
esttab,  compress ar2 starlevels(* 0.1 ** 0.05 *** 0.01)  b(%4.3f) se(%4.3f) label keep(spell1 spell2 spell3 spell_elu1 spell_elu2 spell_elu3 order_in_spell1 order_in_spell2 order_in_spell3 order_in_spell4 order_in_spell1_elu order_in_spell2_elu order_in_spell3_elu order_in_spell4_elu elected order_ju_cr order_ju) mtitles(OLS "|distance|" q10 q25 q50 q75 q90)
eststo clear 

restore



****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
** Table 5: Mechanisms of convergence: learning from local actors
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************



****************************************************************************************
****************************************************************************************
*** columns 1 and 2: Prop senior judges

drop transit*

sort  year month districtID IDjudge
gen transit=IDjudge!=IDjudge[_n-1] | districtID!=districtID[_n-1] | year!=year[_n-1] | month!=month[_n-1]
by year month districtID: egen nb_judge_month_di=sum(transit)
gen transit2=transit
replace transit2=0 if youngjudge==1
by year month districtID: egen nb_old_judge_month_di=sum(transit2)
gen transit3=transit
replace transit3=0 if  elected==0
by year month districtID: egen nb_elec_judge_month_di=sum(transit3)
gen transit4=transit
replace transit4=0 if  elected==0 | youngjudge==0
by year month districtID: egen nb_elec_young_judge_month_di=sum(transit4)

drop transit*

gen prop_old_month_di=nb_old_judge_month_di/(nb_judge_month_di-1)
gen prop_old_month_di2=(nb_old_judge_month_di+nb_elec_young_judge_month_di-elected)/(nb_judge_month_di-1)


gen order_ju_di_a1=.
gen order_ju_di_elu_a1=.
gen order_ju_di_na1=.
gen order_ju_di_elu_na1=.

label var order_ju_di_a1 "Case number in district*(cv above median)"
label var order_ju_di_elu_a1 "Case number in district*home*(cv above median)"
label var order_ju_di_na1 "Case number in district*(cv below median)"
label var order_ju_di_elu_na1 "Case number in district*home*(cv below median)"


gen order_ju_di_a3=.
gen order_ju_di_elu_a3=.
gen order_ju_di_na3=.
gen order_ju_di_elu_na3=.

label var order_ju_di_a3 "order*(top 25\%)"
label var order_ju_di_elu_a3 "order*elected*(top 25\%)"
label var order_ju_di_na3 "order*(bottom 75\%)"
label var order_ju_di_elu_na3 "order*elected*(bottom 75\%)"


gen above1=.
label var above1 "Above median"
gen above3=.
label var above3 "Top 25\%"


preserve
keep if youngjudge==1
keep if max_in_distr>400
keep if order_ju_di<=400


sort IDjudge order_ju_di
bys IDjudge districtID: egen prop1=sum(prop_old_month_di)
bys IDjudge districtID: egen prop2=sum(prop_old_month_di2)

sum prop1, d
replace above1=prop1>r(p50)
* not signif with p75, opposit sign with p50

replace order_ju_di_a1=order_ju_di*above1
replace order_ju_di_elu_a1=order_ju_di_elu*above1
replace order_ju_di_na1=order_ju_di*(1-above1)
replace order_ju_di_elu_na1=order_ju_di_elu*(1-above1)

sum abs_distance3
local mean=r(mean)
local sd=r(sd)


 eststo: reg abs_distance3 order_ju_di_a1 order_ju_di_na1 order_ju_di_elu_a1 order_ju_di_elu_na1   elected order_ju_cr order_ju above1,  cluster(IDjudge) a(IDjudge)
test order_ju_di_a1=order_ju_di_na1
estadd scalar pval = r(p)
local pval=r(p)

restore



** 75%

preserve
keep if youngjudge==1
keep if max_in_distr>400
keep if order_ju_di<=400


sort IDjudge order_ju_di
bys IDjudge districtID: egen prop1=sum(prop_old_month_di)
bys IDjudge districtID: egen prop2=sum(prop_old_month_di2)

sum prop1, d
replace above3=prop1>r(p75)

replace order_ju_di_a3=order_ju_di*above3
replace order_ju_di_elu_a3=order_ju_di_elu*above3
replace order_ju_di_na3=order_ju_di*(1-above3)
replace order_ju_di_elu_na3=order_ju_di_elu*(1-above3)

sum abs_distance3
local mean=r(mean)
local sd=r(sd)


eststo: reg abs_distance3 order_ju_di_a3 order_ju_di_na3 order_ju_di_elu_a3 order_ju_di_elu_na3  elected order_ju_cr order_ju above3,  cluster(IDjudge) a(IDjudge)
test order_ju_di_a3=order_ju_di_na3
estadd scalar pval=r(p)
local pval=r(p)

restore








****************************************************************************************
****************************************************************************************
*** columns 3 and 4:  Herfindhal Index District Attorney

gen drop_DA=0
replace drop_DA=1 if CRNNAM_DA_true=="BAD"
replace drop_DA=1 if CRNNAM_DA_true==""
replace drop_DA=1 if CRNNAM_DA_true=="ASSISTANT DISTRICT ATTORNEY"




**** reg HI of DA


preserve
keep if youngjudge==1
keep if max_in_distr>400
keep if order_ju_di<=400


bysort districtID IDjudge CRNNAM_DA_true : egen case_DA=sum(id) 
replace case_DA=. if drop_DA==1
gen case_DA_squ=case_DA*case_DA

sort districtID IDjudge CRNNAM_DA_true
replace case_DA_squ=. if CRNNAM_DA_true==CRNNAM_DA_true[_n-1]
bysort districtID IDjudge : egen num=sum(case_DA_squ)

gen transit=drop_DA==0
bysort districtID IDjudge: egen tot=sum(transit) 
gen tot2=tot*tot

gen HI_DA=num/tot2
gen HI_DA_fiability=tot/400


sum HI_DA, d
replace above1=HI_DA>r(p50)

replace order_ju_di_a1=order_ju_di*above1
replace order_ju_di_elu_a1=order_ju_di_elu*above1

replace order_ju_di_na1=order_ju_di*(1-above1)
replace order_ju_di_elu_na1=order_ju_di_elu*(1-above1)

sum abs_distance3
local mean=r(mean)
local sd=r(sd)


 eststo: reg abs_distance3 order_ju_di_a1 order_ju_di_na1 order_ju_di_elu_a1 order_ju_di_elu_na1  elected order_ju_cr order_ju above1,  cluster(IDjudge) a(IDjudge)
test order_ju_di_a1=order_ju_di_na1
estadd scalar pval=r(p)
local pval=r(p)
restore


**** reg HI of DA 75%



preserve
keep if youngjudge==1
keep if max_in_distr>400
keep if order_ju_di<=400


bysort districtID IDjudge CRNNAM_DA_true : egen case_DA=sum(id) 
replace case_DA=. if drop_DA==1
gen case_DA_squ=case_DA*case_DA

sort districtID IDjudge CRNNAM_DA_true
replace case_DA_squ=. if CRNNAM_DA_true==CRNNAM_DA_true[_n-1]
bysort districtID IDjudge : egen num=sum(case_DA_squ)

gen transit=drop_DA==0
bysort districtID IDjudge: egen tot=sum(transit) 
gen tot2=tot*tot

gen HI_DA=num/tot2
gen HI_DA_fiability=tot/400



*drop if HI_DA_fiability<0.5

sum HI_DA, d
replace above3=HI_DA>r(p75)

replace order_ju_di_a3=order_ju_di*above3
replace order_ju_di_elu_a3=order_ju_di_elu*above3

replace order_ju_di_na3=order_ju_di*(1-above3)
replace order_ju_di_elu_na3=order_ju_di_elu*(1-above3)

sum abs_distance3
local mean=r(mean)
local sd=r(sd)


 eststo: reg abs_distance3 order_ju_di_a3 order_ju_di_na3 order_ju_di_elu_a3 order_ju_di_elu_na3  elected order_ju_cr order_ju above3,  cluster(IDjudge) a(IDjudge)
test order_ju_di_a3=order_ju_di_na3
estadd scalar pval=r(p)
local pval=r(p)
restore



****************************************************************************************
****************************************************************************************
*** columns 3 and 4:  Herfindhal Index Defense Attorney


gen drop_AT=0
replace drop_AT=1 if CRNNAM_AT_true=="BAD"
replace drop_AT=1 if CRNNAM_AT_true==""
replace drop_AT=1 if CRNNAM_AT_true=="ASSISTANT DISTRICT ATTORNEY"




**** reg HI of AT


preserve
keep if youngjudge==1
keep if max_in_distr>400
keep if order_ju_di<=400


bysort districtID IDjudge CRNNAM_AT_true : egen case_AT=sum(id) 
replace case_AT=. if drop_AT==1
gen case_AT_squ=case_AT*case_AT

sort districtID IDjudge CRNNAM_AT_true
replace case_AT_squ=. if CRNNAM_AT_true==CRNNAM_AT_true[_n-1]
bysort districtID IDjudge : egen num=sum(case_AT_squ)

gen transit=drop_AT==0
bysort districtID IDjudge: egen tot=sum(transit) 
gen tot2=tot*tot

gen HI_AT=num/tot2
gen HI_AT_fiability=tot/400

sum HI_AT, d
replace above1=HI_AT>r(p50)

replace order_ju_di_a1=order_ju_di*above1
replace order_ju_di_elu_a1=order_ju_di_elu*above1

replace order_ju_di_na1=order_ju_di*(1-above1)
replace order_ju_di_elu_na1=order_ju_di_elu*(1-above1)

sum abs_distance3
local mean=r(mean)
local sd=r(sd)


 eststo: reg abs_distance3 order_ju_di_a1 order_ju_di_na1 order_ju_di_elu_a1 order_ju_di_elu_na1  elected order_ju_cr order_ju above1,  cluster(IDjudge) a(IDjudge)
test order_ju_di_a1=order_ju_di_na1
estadd scalar pval=r(p)
local pval=r(p)
restore


**** reg HI of AT 75%


preserve
keep if youngjudge==1
keep if max_in_distr>400
keep if order_ju_di<=400


bysort districtID IDjudge CRNNAM_AT_true : egen case_AT=sum(id) 
replace case_AT=. if drop_AT==1
gen case_AT_squ=case_AT*case_AT

sort districtID IDjudge CRNNAM_AT_true
replace case_AT_squ=. if CRNNAM_AT_true==CRNNAM_AT_true[_n-1]
bysort districtID IDjudge : egen num=sum(case_AT_squ)

gen transit=drop_AT==0
bysort districtID IDjudge: egen tot=sum(transit) 
gen tot2=tot*tot

gen HI_AT=num/tot2
gen HI_AT_fiability=tot/400

sum HI_AT, d
replace above3=HI_AT>r(p75)

replace order_ju_di_a3=order_ju_di*above3
replace order_ju_di_elu_a3=order_ju_di_elu*above3

replace order_ju_di_na3=order_ju_di*(1-above3)
replace order_ju_di_elu_na3=order_ju_di_elu*(1-above3)

sum abs_distance3
local mean=r(mean)
local sd=r(sd)


 eststo: reg abs_distance3 order_ju_di_a3 order_ju_di_na3 order_ju_di_elu_a3 order_ju_di_elu_na3  elected order_ju_cr order_ju above3,  cluster(IDjudge) a(IDjudge)
test order_ju_di_a3=order_ju_di_na3
estadd scalar pval=r(p)
local pval=r(p)
restore




local regnom    "tab5"
esttab using  `regnom',   b(%4.3f) se(%4.3f)  r2 starlevels({$^{*}$} 0.1 {$^{**}$} 0.05 {$^{***}$} 0.01)  tex  label replace    stats(pval N) mtitles("Proportion old judge in court" "Proportion old judge in court" "HI District Attorney" "HI District Attorney" "HI Defense Attorney"  "HI Defense Attorney")
esttab,  compress ar2 starlevels(* 0.1 ** 0.05 *** 0.01)  b(%4.3f) se(%4.3f) label    stats(pval N) mtitles("Proportion old judge in court" "Proportion old judge in court" "HI District Attorney" "HI District Attorney" "HI Defense Attorney"  "HI Defense Attorney")
eststo clear 





****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
** Table 6: correlates of the local sentencing norm
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************


preserve 


drop if districtID==.
drop _merge
 

********************************************
**** construct variables
**************************************************

**** Herfindhal Index AT
drop drop_AT drop_DA

gen drop_AT=0
replace drop_AT=1 if CRNNAM_AT_true=="BAD"
replace drop_AT=1 if CRNNAM_AT_true==""


***** construct market share per year per district of each lawyer

gen id=1
bysort districtID year CRNNAM_AT_true : gen case_AT_yeart=sum(id) if drop_AT==0
bysort districtID year CRNNAM_AT_true : egen case_AT_year=max(case_AT_yeart) if drop_AT==0
bysort districtID year: gen case_AT_dis_yeart=sum(id) 
bysort districtID year: egen case_AT_dis_year=max(case_AT_dis_yeart) 
gen mkt_share_year_AT = ( 100 *case_AT_year / case_AT_dis_year)^2 if drop_AT==0
sort districtID year CRNNAM_AT_true 
replace mkt_share_year=. if CRNNAM_AT_true[_n-1] ==CRNNAM_AT_true & districtID[_n-1]==districtID  &  year[_n-1] == year
bysort districtID year: egen HI_year_AT= sum(mkt_share_year_AT)

sort districtID year
replace HI_year_AT= . if districtID[_n-1]==districtID  &  year[_n-1] == year
replace HI_year_AT=. if HI_year_AT==10000
replace HI_year_AT=. if HI_year_AT==0

bysort districtID: egen HI_dis_AT= mean(HI_year_AT)



**** Herfindhal Index DA

gen drop_DA=0
replace drop_DA=1 if CRNNAM_DA_true=="BAD"
replace drop_DA=1 if CRNNAM_DA_true==""
replace drop_DA=1 if CRNNAM_DA_true=="ASSISTANT DISTRICT ATTORNEY"


bysort districtID year CRNNAM_DA_true : gen case_DA_yeart=sum(id) if drop_DA==0
bysort districtID year CRNNAM_DA_true : egen case_DA_year=max(case_DA_yeart) if drop_DA==0
bysort districtID year: gen case_DA_dis_yeart=sum(id) 
bysort districtID year: egen case_DA_dis_year=max(case_DA_dis_yeart) 
gen mkt_share_year = ( 100 *case_DA_year / case_DA_dis_year)^2 if drop_DA==0
sort districtID year CRNNAM_DA_true 
replace mkt_share_year=. if CRNNAM_DA_true[_n-1] ==CRNNAM_DA_true & districtID[_n-1]==districtID  &  year[_n-1] == year
bysort districtID year: egen HI_year= sum(mkt_share_year)

sort districtID year
replace HI_year= . if districtID[_n-1]==districtID  &  year[_n-1] == year
replace HI_year=. if HI_year==10000
replace HI_year=. if HI_year==0

bysort districtID: egen HI_dis_DA= mean(HI_year)



**** Herfindhal Index AT by crime

bysort districtID IDcrime_trunc year CRNNAM_AT_true : gen case_AT_yeart_CR=sum(id) if drop_AT==0
bysort districtID IDcrime_trunc year CRNNAM_AT_true : egen case_AT_year_CR=max(case_AT_yeart_CR) if drop_AT==0
bysort districtID IDcrime_trunc year: gen case_AT_dis_yeart_CR=sum(id) 
bysort districtID IDcrime_trunc year: egen case_AT_dis_year_CR=max(case_AT_dis_yeart_CR) 
gen mkt_share_year_AT_CR = ( 100 *case_AT_year_CR / case_AT_dis_year_CR)^2 if drop_AT==0
sort districtID IDcrime_trunc year CRNNAM_AT_true 
replace mkt_share_year_AT_CR=. if CRNNAM_AT_true[_n-1] ==CRNNAM_AT_true & districtID[_n-1]==districtID  &  year[_n-1] == year
bysort districtID IDcrime_trunc year: egen HI_year_AT_CR= sum(mkt_share_year_AT_CR)

sort districtID IDcrime_trunc year
replace HI_year_AT_CR= . if districtID[_n-1]==districtID  &  year[_n-1] == year & IDcrime_trunc[_n-1] ==IDcrime_trunc
replace HI_year_AT_CR=. if HI_year_AT_CR==10000
replace HI_year_AT_CR=. if HI_year_AT_CR==0

bysort districtID IDcrime_trunc: egen HI_dis_AT_CR= mean(HI_year_AT_CR)

*bysort districtID: gen case_nbi=sum(id)
*bysort districtID: egen case_nb=max(case_nb)



**** Herfindhal Index DA by crime

bysort districtID IDcrime_trunc year CRNNAM_DA_true : gen case_DA_yeart_CR=sum(id) if drop_DA==0
bysort districtID IDcrime_trunc year CRNNAM_DA_true : egen case_DA_year_CR=max(case_DA_yeart_CR) if drop_DA==0
bysort districtID IDcrime_trunc year: gen case_DA_dis_yeart_CR=sum(id) 
bysort districtID IDcrime_trunc year: egen case_DA_dis_year_CR=max(case_DA_dis_yeart_CR) 
gen mkt_share_year_DA_CR = ( 100 *case_DA_year_CR / case_DA_dis_year_CR)^2 if drop_DA==0
sort districtID IDcrime_trunc year CRNNAM_DA_true 
replace mkt_share_year_DA_CR=. if CRNNAM_DA_true[_n-1] ==CRNNAM_DA_true & districtID[_n-1]==districtID  &  year[_n-1] == year
bysort districtID IDcrime_trunc year: egen HI_year_DA_CR= sum(mkt_share_year_DA_CR)

sort districtID IDcrime_trunc year
replace HI_year_DA_CR= . if districtID[_n-1]==districtID  &  year[_n-1] == year & IDcrime_trunc[_n-1] ==IDcrime_trunc
replace HI_year_DA_CR=. if HI_year_DA_CR==10000
replace HI_year_DA_CR=. if HI_year_DA_CR==0

bysort districtID IDcrime_trunc: egen HI_dis_DA_CR= mean(HI_year_DA_CR)


sort districtID
bys districtID IDcrime_trunc: egen locsent=mean(maxSENT_wind_old)

bys districtID IDcrime_trunc: egen locsent_int=mean(maxSENT_wind_old) if num_times_seen_sofar==1
bys districtID IDcrime_trunc: egen locsent_prime=max(locsent_int)  

label var locsent_prime "sent norm"

 
*******************************************************************
*** Explain mean sentence district /crime 
********************************************************************


gen id3=1
bysort districtID IDcrime_trunc: egen case_cr_dis=sum(id3)
bysort IDcrime_trunc: egen min_dis= min(case_cr_dis)
bysort districtID : egen case_dis=sum(id3)
bysort IDcrime_trunc: egen case_cr=sum(id3)
gen case=sum(id3)
egen case_tot=max(case)

gen crime_prev_dif = 100 *(case_cr_dis / case_dis) - 100 * (case_cr/case_tot)
replace crime_prev_dif=. if districtID==.
replace crime_prev_dif=. if IDcrime_trunc==.



*********************************************************
*** create variable on average recidivism
**********************************************************

bysort districtID IDcrime_trunc: egen recidi_cr_dis=mean(num_times_seen_sofar) 


cd "$data"
merge n:1 districtID  using referenda_crowd
cd "$results"


label var locsent "sent norm"
label var crime_prev_dif "Crime prevalence"
label var HI_dis_AT "HI for attorneys"
label var HI_dis_DA "HI for district attorneys"
label var overcrowd_dis "prison overcrowding"
label var recidi_cr_dis "recidivsm"

sort districtID IDcrime_trunc
drop if districtID[_n-1]==districtID  & IDcrime_trunc[_n-1]!=    IDcrime_trunc

drop if min_dis<10

local regnom    "tab6"

qui eststo:  reg locsent   i.IDcrime_trunc  i.districtID   crime_prev_dif  if districtID[_n-1]!=districtID | IDcrime_trunc[_n-1]!=    IDcrime_trunc  , cluster(districtID)
qui eststo:  reg locsent   i.IDcrime_trunc i.districtID    HI_dis_AT_CR  if districtID[_n-1]!=districtID | IDcrime_trunc[_n-1]!=    IDcrime_trunc  , cluster(districtID)
qui eststo:  reg locsent   i.IDcrime_trunc i.districtID    HI_dis_DA_CR  if districtID[_n-1]!=districtID | IDcrime_trunc[_n-1]!=    IDcrime_trunc  , cluster(districtID)
qui eststo:  reg locsent   i.IDcrime_trunc i.districtID   crime_prev HI_dis_AT_CR  HI_dis_DA_CR  if districtID[_n-1]!=districtID | IDcrime_trunc[_n-1]!=    IDcrime_trunc  , cluster(districtID)
qui eststo:  reg locsent_prime   i.IDcrime_trunc  i.districtID    recidi_cr_dis  if districtID[_n-1]!=districtID | IDcrime_trunc[_n-1]!=    IDcrime_trunc  , cluster(districtID)
esttab using  `regnom',  b(%4.3f) se(%4.3f) ar2 starlevels({$^{*}$} 0.1 {$^{**}$} 0.05 {$^{***}$} 0.01)  tex  label replace keep( crime_prev_dif      HI_dis_AT_CR HI_dis_DA_CR recidi_cr_dis) 
esttab,  compress ar2 starlevels(* 0.1 ** 0.05 *** 0.01)  b(%4.3f) se(%4.3f) label    keep( crime_prev_dif      HI_dis_AT_CR HI_dis_DA_CR recidi_cr_dis) 
eststo clear 


restore





****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
* Appendix Table 9: Effect of order of decision by district and crime
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************





preserve
bys IDjudge districtID IDcrime: egen max_in_distr_cr=max(order_ju_di_cr)

keep if youngjudge==1
keep if max_in_distr_cr>200
keep if order_ju_di_cr<=200

gen order_ju_di_cr_elu = order_ju_di_cr *elected

sum distance3
local mean=r(mean)
local sd=r(sd)

qui eststo: reg distance3   order_ju_di_cr order_ju_di_cr_elu elected order_ju_cr order_ju,  cluster(IDjudge) a(IDjudge)

sum abs_distance3
local mean=r(mean)
local sd=r(sd)

qui eststo: reg abs_distance3   order_ju_di_cr order_ju_di_cr_elu elected order_ju_cr order_ju,  cluster(IDjudge) a(IDjudge)

tab IDjudge, g(juge_identifier)

foreach x in 10  25  50  75  90{
qui eststo: qreg2 distance3   elected order_ju_cr order_ju_di_cr order_ju_di_cr_elu order_ju juge_identifier*, q(`x') c(IDjudge)
}


local regnom    "tab9"
esttab using  `regnom',   b(%4.3f) se(%4.3f)  r2 starlevels({$^{*}$} 0.1 {$^{**}$} 0.05 {$^{***}$} 0.01)  tex  label replace  keep(elected order_ju_cr order_ju order_ju_di_cr order_ju_di_cr_elu) mtitles(OLS "|distance|" q10 q25 q50 q75 q90 )
esttab,  compress ar2 starlevels(* 0.1 ** 0.05 *** 0.01)  b(%4.3f) se(%4.3f) label   keep(  elected order_ju_cr order_ju order_ju_di_cr order_ju_di_cr_elu) mtitles(OLS "|distance|" q10 q25 q50 q75 q90)
eststo clear 

restore


****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
* Figure 5: District experience and sentencing
* Appendix Table 7: Quantile regressions
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************



preserve
keep if youngjudge==1
keep if max_in_distr>400
keep if order_ju_di<=400

forval x=1/7{
gen gpe`x'_NE=order_ju_di<=`x'*50 & order_ju_di>(`x'-1)*50 & elected==0
local z=`x'*50
local w=(`x'-1)*50+1
label var gpe`x'_NE "Case `w'-`z' - Non home district"
}
forval x=1/7{
gen gpe`x'_E=order_ju_di<=`x'*50 & order_ju_di>(`x'-1)*50 & elected==1
local z=`x'*50
local w=(`x'-1)*50+1
label var gpe`x'_E "Case `w'-`z' - Home district"
}

gen order=.
gen elu=.
gen coeff_10=.
gen coeff_20=.
gen coeff_50=.
gen coeff_80=.
gen coeff_90=.
gen se_10=.
gen se_20=.
gen se_50=.
gen se_80=.
gen se_90=.

tab IDjudge, g(juge_identifier)

foreach z in 10  50  90 {
*qui eststo: qreg distance3 gpe1_NE-gpe7_E elected order_ju_cr order_ju juge_identifier*, q(`z') iter(500) vce(robust)
qui eststo: qreg2 distance3 gpe1_NE-gpe7_E elected order_ju_cr order_ju juge_identifier*, q(`z') c(IDjudge)


local i=1
forval x=1/7{
replace order=`x' if _n==`i'
replace elu=0 if _n==`i'

replace coeff_`z'=_b[gpe`x'_NE] if _n==`i'
replace se_`z'=_se[gpe`x'_NE] if _n==`i'
local i=`i'+1

replace order=`x' if _n==`i'
replace elu=1 if _n==`i'

replace coeff_`z'=_b[gpe`x'_E] if _n==`i'
replace se_`z'=_se[gpe`x'_E] if _n==`i'
local i=`i'+1
}

gen hipeine_`z'=coeff_`z'+1.64*se_`z'
gen lowpeine_`z'=coeff_`z'-1.64*se_`z'
}

gen order2=order+0.1
gen order3=order+0.05
tw(line coeff_10 order if elu==0)(line coeff_50 order3 if elu==0)(line coeff_90 order2 if elu==0)/*
*/(rcap hipeine_10 lowpeine_10 order if elu==0)(rcap hipeine_50 lowpeine_50 order3 if elu==0)(rcap hipeine_90 lowpeine_90 order2 if elu==0),/*
*/legend(order(1 "10th percentile" 2 "Median" 3 "90th percentile")) ytitle("Coefficients") /*
*/ title("Non-home Districts") xlabel(1 "1-50" 2 "51-100" 3 "101-150" 4 "151-200" 5 "201-250" 6 "251-300" 7 "301-351")/*
*/ xtitle("Number of Decisions in District") yline(0)/*
*/graphregion(icolor(white) color(white) margin(medlarge)) scheme(s2mono) plotregion(lcolor(black) lwidth(vthin))  
graph export "fig5_Non_Home_district.pdf", replace

tw(line coeff_10 order if elu==1)(line coeff_50 order3 if elu==1)(line coeff_90 order2 if elu==1)/*
*/(rcap hipeine_10 lowpeine_10 order if elu==1)(rcap hipeine_50 lowpeine_50 order3 if elu==1)(rcap hipeine_90 lowpeine_90 order2 if elu==1),/*
*/legend(order(1 "10th percentile" 2 "Median" 3 "90th percentile")) ytitle("Coefficients") /*
*/ title("Home District") xlabel(1 "1-50" 2 "51-100" 3 "101-150" 4 "151-200" 5 "201-250" 6 "251-300" 7 "301-351")/*
*/ xtitle("Number of Decisions in District") yline(0) ylabel(-200 "-200" -100 "-100" 0 "0" 100 "100" 200 "200" 300 "300")/*
*/graphregion(icolor(white) color(white) margin(medlarge)) scheme(s2mono) plotregion(lcolor(black) lwidth(vthin))  
graph export "fig5_Home_district.pdf", replace



local regnom    "tab7"
esttab using  `regnom',   b(%4.3f) se(%4.3f) /*ar2*/ starlevels({$^{*}$} 0.1 {$^{**}$} 0.05 {$^{***}$} 0.01)  tex  label replace   mtitles("q10"  "q50"  "q90")/*
*/ keep(gpe1_NE gpe2_NE gpe3_NE gpe4_NE gpe5_NE gpe6_NE gpe1_NE gpe2_E gpe3_E gpe4_E gpe5_E gpe6_E  gpe7_E)
esttab,  compress /*ar2*/ starlevels(* 0.1 ** 0.05 *** 0.01)  b(%4.3f) se(%4.3f) label    mtitles("q10"  "q50"  "q90") /*
*/ keep(gpe1_NE gpe2_NE gpe3_NE gpe4_NE gpe5_NE gpe6_NE gpe1_NE gpe2_E gpe3_E gpe4_E gpe5_E gpe6_E  gpe7_E)
eststo clear 

restore





****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
* Figure 6: Comparison of home and non-home districts
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************



preserve
keep if youngjudge==1
keep if max_in_distr>400
keep if order_ju_di<=400

replace order_ju_di=0 if elected==1

gen quantile=.
gen coef_NE=.
gen se_NE=.
gen coef_E=.
gen se_E=.

tab IDjudge, g(juge_identifier)

local i=1
forval x=5(5)95{
replace quantile=`x' if _n==`i'

qreg2 distance3 order_ju_di order_ju_di_elu elected order_ju_cr order_ju juge_identifier*, q(`x') c(IDjudge)
replace coef_NE=_b[order_ju_di] if _n==`i'
replace se_NE=_se[order_ju_di] if _n==`i'
replace coef_E=_b[order_ju_di_elu] if _n==`i'
replace se_E=_se[order_ju_di_elu] if _n==`i'
local i=1+`i'
}

gen hipeine_NE=coef_NE+1.64*se_NE
gen lowpeine_NE=coef_NE-1.64*se_NE

gen hipeine_E=coef_E+1.64*se_E
gen lowpeine_E=coef_E-1.64*se_E

gen quantile2=quantile+0.5
tw(line coef_NE quantile)(line coef_E quantile2)/*
*/(rcap hipeine_NE lowpeine_NE quantile)(rcap hipeine_E lowpeine_E quantile2, lpattern(dash)),/*
*/legend(order(1 "Non-home Districts" 2 "Home District")) ytitle("Coefficient") /*
 title("Effect of the order of decision on the distance" "for different quantiles")
*/ xtitle("Quantile") yline(0)/*
*/graphregion(icolor(white) color(white) margin(medlarge)) scheme(s2mono) plotregion(lcolor(black) lwidth(vthin))  
graph export "fig6.pdf", replace

restore



****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
* Figure 7: District experience and sentencing for junior and senior judges
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************



preserve
keep if elected==0
keep if max_in_distr>400
keep if order_ju_di<=400

forval x=1/7{
gen gpe`x'_NE=order_ju_di<=`x'*50 & order_ju_di>(`x'-1)*50 & elected==0

}

gen order=.
gen coeff_young=.
gen se_young=.
gen coeff_old=.
gen se_old=.
gen coeff_old2=.
gen se_old2=.

reg abs_distance4 gpe1_NE-gpe7_NE elected order_ju_cr order_ju if youngjudge==1,  vce(cluster IDjudge) a(IDjudge)
local i=1
forval x=1/7{
replace order=`x' if _n==`i'

replace coeff_young=_b[gpe`x'_NE] if _n==`i'
replace se_young=_se[gpe`x'_NE] if _n==`i'

local i=`i'+1
}

reg abs_distance4 gpe1_NE-gpe7_NE elected order_ju_cr order_ju if youngjudge==0,  vce(cluster IDjudge) a(IDjudge)
local i=1
forval x=1/7{
replace order=`x' if _n==`i'

replace coeff_old=_b[gpe`x'_NE] if _n==`i'
replace se_old=_se[gpe`x'_NE] if _n==`i'

local i=`i'+1
}

reg abs_distance4 gpe1_NE-gpe7_NE elected order_ju_cr order_ju if youngjudge==0 & StartYear<1993,  vce(cluster IDjudge) a(IDjudge)
local i=1
forval x=1/7{
replace order=`x' if _n==`i'

replace coeff_old2=_b[gpe`x'_NE] if _n==`i'
replace se_old2=_se[gpe`x'_NE] if _n==`i'

local i=`i'+1
}

gen hipeine_young=coeff_young+1.64*se_young
gen lowpeine_young=coeff_young-1.64*se_young
gen hipeine_old=coeff_old+1.64*se_old
gen lowpeine_old=coeff_old-1.64*se_old
gen hipeine_old2=coeff_old2+1.64*se_old2
gen lowpeine_old2=coeff_old2-1.64*se_old2

gen order2=order+0.1
gen order3=order+0.2
tw(line coeff_young order)(line coeff_old order2 )(line coeff_old2 order3 )/*
*/(rcap hipeine_young lowpeine_young order )(rcap hipeine_old lowpeine_old order2)(rcap hipeine_old2 lowpeine_old2 order3),/*
*/legend(order(1 "Junior judges" 2 "Senior judges" 3 "Senior judges start before 93")) ytitle("Coefficients")/*
 title("Effect of the order of decision in the district" "on the absolute value of the distance")
*/  xlabel(1 "1-50" 2 "51-100" 3 "101-150" 4 "151-200" 5 "201-250" 6 "251-300" 7 "301-351")/*
*/ xtitle("Number of Decisions in District") yline(0) /*
*/graphregion(icolor(white) color(white) margin(medlarge)) scheme(s2mono) plotregion(lcolor(black) lwidth(vthin))  
graph export "fig7.pdf", replace

restore




****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
* Figure 8: Average distance between decisions and local norm
* Appendix Table 10: Mechanisms of convergence: difference between districts
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************




****************************************************************************************
****************************************************************************************
*  col 1:  distance in norm

drop _merge
preserve
keep if youngjudge==0
keep  districtID IDcrime maxSENT_wind
collapse (mean) maxSENT_wind, by(districtID IDcrime)
rename maxSENT_wind mean_sent
save mean_sent_di_cr, replace
restore



preserve
gen transit=districtID if elected==1
bys IDjudge: egen dist_e=mean(transit)

keep if youngjudge==1
keep if max_in_distr>400
keep if order_ju_di<=400

merge m:1 districtID IDcrime using mean_sent_di_cr
drop if _merge==2
rename mean_sent mean_current
drop _merge

rename districtID districtID_o
rename dist_e districtID
merge m:1 districtID IDcrime using mean_sent_di_cr
drop if _merge==2
rename mean_sent mean_elu
rename  districtID dist_e
rename districtID_o districtID


gen distance_home_non=mean_current-mean_elu
gen abs_distance_home_non=abs(distance_home_non)

bys IDjudge districtID: egen ln_vs_hn=mean(abs_distance_home_non)


centile ln_vs_hn if elected==0 & youngjudge==1, centile(33 66)

gen top=ln_vs_hn>r(c_2) & ln_vs_hn!=.
gen middle=ln_vs_hn>r(c_1) & ln_vs_hn<=r(c_2) & ln_vs_hn!=.
gen bottom=ln_vs_hn<=r(c_1) & ln_vs_hn!=. & ln_vs_hn!=0

gen order_ju_di_t=order_ju_di*top*(1-elected)
gen order_ju_di_m=order_ju_di*middle*(1-elected)
gen order_ju_di_b=order_ju_di*bottom*(1-elected)

sum abs_distance3
local mean=r(mean)
local sd=r(sd)


eststo: reg abs_distance3 order_ju_di_t order_ju_di_m order_ju_di_b order_ju_di_elu order_ju_cr order_ju  top middle bottom if elected==1 | ln_vs_hn!=.,  cluster(IDjudge) a(IDjudge)


label var order_ju_di_t "Case number in district*Norm difference:High"
label var order_ju_di_m "Case number in district*Norm difference:Middle"
label var order_ju_di_b "Case number in district*Norm difference:Low"
label var order_ju_di_elu "Case number in district*Elected"

label var top "Norm difference:High"
label var middle "Norm difference:Middle"
label var bottom "Norm difference:Low"


****************************************************************************************
****************************************************************************************
* Figure 8


gen gpe=.
gen mean=.
gen ab=.
gen high=.
gen low=.

local w=1
local i=1
sum abs_distance3 if elected==1 & youngjudge==1 & order_ju_di<=100
replace gpe=`w'*5-4 if _n==`i'
replace ab=2 if _n==`i'
replace mean=r(mean) if _n==`i'
replace high=r(mean)+invttail(r(N)-1,0.1)*(r(sd) / sqrt(r(N))) if _n==`i'
replace low=r(mean)-invttail(r(N)-1,0.1)*(r(sd) / sqrt(r(N))) if _n==`i'
local i=`i'+1

sum abs_distance3 if bottom==1 & elected==0 & youngjudge==1 & order_ju_di<=100
replace gpe=`w'*5-3 if _n==`i'
replace ab=1 if _n==`i'
replace mean=r(mean) if _n==`i'
replace high=r(mean)+invttail(r(N)-1,0.1)*(r(sd) / sqrt(r(N))) if _n==`i'
replace low=r(mean)-invttail(r(N)-1,0.1)*(r(sd) / sqrt(r(N))) if _n==`i'
local i=`i'+1

sum abs_distance3 if middle==1 & elected==0 & youngjudge==1  & order_ju_di<=100
replace gpe=`w'*5-2 if _n==`i'
replace ab=0 if _n==`i'
replace mean=r(mean) if _n==`i'
replace high=r(mean)+invttail(r(N)-1,0.1)*(r(sd) / sqrt(r(N))) if _n==`i'
replace low=r(mean)-invttail(r(N)-1,0.1)*(r(sd) / sqrt(r(N))) if _n==`i'
local i=`i'+1

sum abs_distance3 if top==1 & elected==0 & youngjudge==1  & order_ju_di<=100
replace gpe=`w'*5-1 if _n==`i'
replace ab=-1 if _n==`i'
replace mean=r(mean) if _n==`i'
replace high=r(mean)+invttail(r(N)-1,0.1)*(r(sd) / sqrt(r(N))) if _n==`i'
replace low=r(mean)-invttail(r(N)-1,0.1)*(r(sd) / sqrt(r(N))) if _n==`i'
local i=`i'+1



tw(bar mean gpe if ab==2, color(black))(bar mean gpe if ab==1, color(gs8))(bar mean gpe if ab==0, color(gs14))(bar mean gpe if ab==-1, fcolor(none) lcolor(black))(rcap high low gpe),/*
*/legend(off) ytitle("Average over first 100 decisions of""|Distance Between Sentence and Local Norm|") xlabel(1 "Home" 2 "Close" 3 "Middle" 4 "Far")/*
*/ xtitle("Difference between local norm and norm in home district") title("") legend(off) /*
*/graphregion(icolor(white) color(white) margin(medlarge))  plotregion(lcolor(black) lwidth(vthin)) 
graph export "fig8.pdf", replace
restore


****************************************************************************************
****************************************************************************************
* Table 10 col2: geographical distance


cd "$data"
merge m:1 electiondistrict_k using election_district_correction
drop if _merge==2

drop _merge
merge m:1 district close using map
drop if _merge==2
drop close
gen close=_merge==3
drop _merge


cd "$results"


gen order_ju_di_close=.
gen order_ju_di_noclose=.
label var order_ju_di_close "Case number in district*adjoining district"
label var order_ju_di_noclose "Case number in district*non adjoining district"
label var close "Adjoining district"
label var order_ju "Case number"

preserve
keep if youngjudge==1
keep if max_in_distr>400
keep if order_ju_di<=400

replace order_ju_di_close=order_ju_di*close*(1-elected)
replace order_ju_di_noclose=order_ju_di*(1-close)*(1-elected)

sum abs_distance3
local mean=r(mean)
local sd=r(sd)

eststo: reg abs_distance3 order_ju_di_close order_ju_di_noclose order_ju_di_elu   elected order_ju_cr order_ju close,  cluster(IDjudge) a(IDjudge)
restore


local regnom    "tab10"
esttab using  `regnom',   b(%4.3f) se(%4.3f)  r2 starlevels({$^{*}$} 0.1 {$^{**}$} 0.05 {$^{***}$} 0.01)  tex  label replace    stats( N) 
esttab,  compress ar2 starlevels(* 0.1 ** 0.05 *** 0.01)  b(%4.3f) se(%4.3f) label    stats( N) 
eststo clear 



****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
*Figure 9: Local sentencing norm and district level variations
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************



preserve
cd "$data"
merge n:1 districtID  using referenda_crowd
drop _merge
cd "$results"


duplicates drop districtID, force

gen ref1996b=ref1996*100
gen ref2004b=ref2004*100
gen ref2010b=ref2010*100
gen ref2014b=ref2014*100


label var pres "Vote share democrat presidential election"
label var ref1996b "Results referundum 1996"
label var ref2004b "Results referundum 2004"
label var ref2010b "Results referundum 2010"
label var ref2014b "Results referundum 2014"
label var overcrowd_dis "Overcrowding"


foreach x in pres  ref2004b  ref2014b overcrowd_dis {

tw(lfitci mean_di_cr_out `x' if mean_di_cr_out!=. & `x'!=.)(scatter mean_di_cr_out `x' if mean_di_cr_out!=. & `x'!=.)/*
*/, ytitle("Average sentence at the district level")/*
*/ title("") legend(off) /*
*/graphregion(icolor(white) color(white) margin(medlarge))  plotregion(lcolor(black) lwidth(vthin)) 
graph export "fig9_`x'.pdf", replace
}
restore



****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
* Appendix Figure 10:District level variations in socio-demographic characteristics of the defendants
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************




preserve

gen di=.
gen div=.

foreach x of var   first_offense_flag  femaleflag blackflag    age {
gen mean_`x'=.
gen high_`x'=.
gen low_`x'=.
local i=1
forval y=1/50{
replace di=`y' if _n==`i'
sum division if districtID==`y'
replace div=r(mean) if _n==`i'
sum `x' if districtID==`y'
replace mean_`x'=r(mean) if _n==`i'
replace high_`x'=r(mean)+invttail(r(N)-1,0.05)*(r(sd) / sqrt(r(N))) if _n==`i'
replace low_`x'=r(mean)-invttail(r(N)-1,0.05)*(r(sd) / sqrt(r(N))) if _n==`i'
local i=`i'+1
}

}

sort div di
replace di=_n if div!=.

tw(bar mean_femaleflag di if div==1, color(black))(bar mean_femaleflag di if div==2, color(gs12))(bar mean_femaleflag di if div==3, color(black))/*
*/(bar mean_femaleflag di if div==4, color(gs12))(bar mean_femaleflag di if div==5, color(black))(bar mean_femaleflag di if div==6, color(gs12))/*
*/(bar mean_femaleflag di if div==7, color(black))(bar mean_femaleflag di if div==8, color(gs12))(rcap high_femaleflag low_femaleflag di)/*
*/, xtitle("Distict ID") ytitle("Mean female")/*
*/legend(off) title(Proportion female)/*
*/ graphregion(icolor(white) color(white) margin(medlarge)) plotregion(lcolor(black) lwidth(vthin))
graph export fig10_female.pdf, replace

tw(bar mean_blackflag di if div==1, color(black))(bar mean_blackflag di if div==2, color(gs12))(bar mean_blackflag di if div==3, color(black))/*
*/(bar mean_blackflag di if div==4, color(gs12))(bar mean_blackflag di if div==5, color(black))(bar mean_blackflag di if div==6, color(gs12))/*
*/(bar mean_blackflag di if div==7, color(black))(bar mean_blackflag di if div==8, color(gs12))(rcap high_blackflag low_blackflag di)/*
*/, xtitle("Distict ID") ytitle("Mean black")/*
*/legend(off) title(Proportion black)/*
*/ graphregion(icolor(white) color(white) margin(medlarge)) plotregion(lcolor(black) lwidth(vthin))
graph export fig10_black.pdf, replace

tw(bar mean_age di if div==1, color(black))(bar mean_age di if div==2, color(gs12))(bar mean_age di if div==3, color(black))/*
*/(bar mean_age di if div==4, color(gs12))(bar mean_age di if div==5, color(black))(bar mean_age di if div==6, color(gs12))/*
*/(bar mean_age di if div==7, color(black))(bar mean_age di if div==8, color(gs12))(rcap high_age low_age di)/*
*/, xtitle("Distict ID") ytitle("Mean age")/*
*/legend(off)  title(Mean age)/*
*/ graphregion(icolor(white) color(white) margin(medlarge)) plotregion(lcolor(black) lwidth(vthin))
graph export fig10_age.pdf, replace

tw(bar mean_first_offense_flag di if div==1, color(black))(bar mean_first_offense_flag di if div==2, color(gs12))(bar mean_first_offense_flag di if div==3, color(black))/*
*/(bar mean_first_offense_flag di if div==4, color(gs12))(bar mean_first_offense_flag di if div==5, color(black))(bar mean_first_offense_flag di if div==6, color(gs12))/*
*/(bar mean_first_offense_flag di if div==7, color(black))(bar mean_first_offense_flag di if div==8, color(gs12))(rcap high_first_offense_flag low_first_offense_flag di)/*
*/, xtitle("Distict ID") ytitle("Mean first offender")/*
*/legend(off) title(Proportion first offender)/*
*/ graphregion(icolor(white) color(white) margin(medlarge)) plotregion(lcolor(black) lwidth(vthin))
graph export fig10_first_offense.pdf, replace
restore



****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
* Appendix Figure 11: District FE
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************




preserve

drop if person_name_group==.

bysort  person_name_group: gen id_person=_n
bysort  person_name_group: egen nber_data=max(id_person)
count if nber_data>1 & nber_data==id_person

sort person_name_group districtID
gen change=(districtID!=districtID[_n-1])
bysort  person_name_group: egen sum_chge=sum(change)
count if sum_chge>1 &  nber_data==id_person

drop if nber_data==1
drop if sum_chge==1


tab districtID, g(districtID)
gen di=.
gen mean_all=.
gen high_all=.
gen low_all=.

reghdfe maxSENT_wind districtID2-districtID50 $fecontrol , a(IDcrime_trunc  person_name_group) 


local i=1
forval x=2/50{
replace di=`x' if _n==`i'
replace mean_all=_b[districtID`x'] if _n==`i'
replace high_all=_b[districtID`x']+1.64*_se[districtID`x'] if _n==`i'
replace low_all=_b[districtID`x']-1.64*_se[districtID`x'] if _n==`i'
local i=`i'+1
}


tw(bar mean_all di)(rcap high_all low_all di)/*
*/, xtitle("Distict ID") ytitle("Mean sentence (judges starting before 1998)")/*
*/legend(off)/*
*/ graphregion(icolor(white) color(white) margin(medlarge)) plotregion(lcolor(black) lwidth(vthin))/*
*/ title("All crimes")
graph export fig11.pdf, replace

restore



****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
* Appendix Figure 12 (active sentence: District experience and sentencing using active sentences
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************



preserve
keep if youngjudge==1
keep if max_in_distr>400
keep if order_ju_di<=400

forval x=1/7{
gen gpe`x'_NE=order_ju_di<=`x'*50 & order_ju_di>(`x'-1)*50 & elected==0
local z=`x'*50
local w=(`x'-1)*50+1
label var gpe`x'_NE "Case `w'-`z' - Non home district"
}
forval x=1/7{
gen gpe`x'_E=order_ju_di<=`x'*50 & order_ju_di>(`x'-1)*50 & elected==1
local z=`x'*50
local w=(`x'-1)*50+1
label var gpe`x'_E "Case `w'-`z' - Home district"
}

gen order=.
gen elu=.
gen coeff_10=.
gen coeff_20=.
gen coeff_50=.
gen coeff_80=.
gen coeff_90=.
gen se_10=.
gen se_20=.
gen se_50=.
gen se_80=.
gen se_90=.

tab IDjudge, g(juge_identifier)

foreach z in 10  50  90 {
*qreg distance8 gpe1_NE-gpe7_E elected order_ju_cr order_ju juge_identifier*, q(`z')  iter(500) vce(robust)
qreg2 distance8 gpe1_NE-gpe7_E elected order_ju_cr order_ju juge_identifier*, q(`z')  c(IDjudge)

local i=1
forval x=1/7{
replace order=`x' if _n==`i'
replace elu=0 if _n==`i'

replace coeff_`z'=_b[gpe`x'_NE] if _n==`i'
replace se_`z'=_se[gpe`x'_NE] if _n==`i'
local i=`i'+1

replace order=`x' if _n==`i'
replace elu=1 if _n==`i'

replace coeff_`z'=_b[gpe`x'_E] if _n==`i'
replace se_`z'=_se[gpe`x'_E] if _n==`i'
local i=`i'+1
}

gen hipeine_`z'=coeff_`z'+1.64*se_`z'
gen lowpeine_`z'=coeff_`z'-1.64*se_`z'
}

gen order2=order+0.1
gen order3=order+0.05
tw(line coeff_10 order if elu==0)(line coeff_50 order3 if elu==0)(line coeff_90 order2 if elu==0)/*
*/(rcap hipeine_10 lowpeine_10 order if elu==0)(rcap hipeine_50 lowpeine_50 order3 if elu==0)(rcap hipeine_90 lowpeine_90 order2 if elu==0),/*
*/legend(order(1 "10th percentile" 2 "Median" 3 "90th percentile")) ytitle("Coefficients") /*
*/ title("Non-home Districts") xlabel(1 "1-50" 2 "51-100" 3 "101-150" 4 "151-200" 5 "201-250" 6 "251-300" 7 "301-351")/*
*/ xtitle("Number of Decisions in District") yline(0)/*
*/graphregion(icolor(white) color(white) margin(medlarge)) scheme(s2mono) plotregion(lcolor(black) lwidth(vthin))  
graph export "fig12_Non_Home_district.pdf", replace

tw(line coeff_10 order if elu==1)(line coeff_50 order3 if elu==1)(line coeff_90 order2 if elu==1)/*
*/(rcap hipeine_10 lowpeine_10 order if elu==1)(rcap hipeine_50 lowpeine_50 order3 if elu==1)(rcap hipeine_90 lowpeine_90 order2 if elu==1),/*
*/legend(order(1 "10th percentile" 2 "Median" 3 "90th percentile")) ytitle("Coefficients") /*
*/ title("Home District") xlabel(1 "1-50" 2 "51-100" 3 "101-150" 4 "151-200" 5 "201-250" 6 "251-300" 7 "301-351")/*
*/ xtitle("Number of Decisions in District") yline(0) ylabel(-200 "-200" 0 "0" 200 "200" 400 "400")/*
*/graphregion(icolor(white) color(white) margin(medlarge)) scheme(s2mono) plotregion(lcolor(black) lwidth(vthin))  
graph export "fig12_Home_district.pdf", replace

restore




****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
* Appendix Figure 13 (active sentence): District experience and sentencing by quantile, for active sentences
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************
****************************************************************************************





preserve
keep if youngjudge==1
keep if max_in_distr>400
keep if order_ju_di<=400

replace order_ju_di=0 if elected==1

gen quantile=.
gen coef_NE=.
gen se_NE=.
gen coef_E=.
gen se_E=.

tab IDjudge, g(juge_identifier)

local i=1
forval x=5(5)95{
replace quantile=`x' if _n==`i'

qreg2 distance8 order_ju_di order_ju_di_elu elected order_ju_cr order_ju juge_identifier*, q(`x') c(IDjudge)
replace coef_NE=_b[order_ju_di] if _n==`i'
replace se_NE=_se[order_ju_di] if _n==`i'
replace coef_E=_b[order_ju_di_elu] if _n==`i'
replace se_E=_se[order_ju_di_elu] if _n==`i'
local i=1+`i'
}

gen hipeine_NE=coef_NE+1.64*se_NE
gen lowpeine_NE=coef_NE-1.64*se_NE

gen hipeine_E=coef_E+1.64*se_E
gen lowpeine_E=coef_E-1.64*se_E

gen quantile2=quantile+0.5
tw(line coef_NE quantile)(line coef_E quantile2)/*
*/(rcap hipeine_NE lowpeine_NE quantile)(rcap hipeine_E lowpeine_E quantile2, lpattern(dash)),/*
*/legend(order(1 "Non-home Districts" 2 "Home District")) ytitle("Coefficient") /*
 title("Effect of the order of decision on the distance" "for different quantiles")
*/ xtitle("Quantile") yline(0)/*
*/graphregion(icolor(white) color(white) margin(medlarge)) scheme(s2mono) plotregion(lcolor(black) lwidth(vthin))  
graph export "fig13.pdf", replace

restore




